# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  include LoginSystem
  
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_src_session_id'
  
  attr_accessor :action_title
  
  def logined
    return !User.find_by_id(session[:user_id]).nil?
  end
  
  def owner_authorized?
    return current_user && current_lough && current_user.owns?(current_lough)
  end
  
  def current_user
    @current_user = session[:user_id] && User.find_by_id(session[:user_id])
  end
  
  def current_lough
    @current_lough = Lough.find_by_screen_name(params[:screen_name])
    if @current_lough.nil?
      @current_lough = current_user && current_user.lough
    end
    return @current_lough
  end
  
  def avatar_url
  	if current_user
  	  @user_display = current_user
  	else
  	  if current_lough
  	  	@user_display = current_lough.user
  	  end
  	end
  	
  	if @user_display && @user_display.avatar
  	    @avatar_url = url_for(:controller => '/photos', :action => 'show', :id => @user_display.avatar.id)
    end
  	
  	return @avatar_url
  end
  
  protected
    
  def check_current_lough
    if current_lough
      return true
    end
    
    return_to_welcome
  end
  
  def return_to_welcome
    redirect_to welcome_url
  end
  
end
